package com.liit.controller.func;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.liit.controller.index.BaseController;
import com.liit.entity.CommodityFormMap;
import com.liit.entity.DictFormMap;
import com.liit.entity.IntegralDetailFormMap;
import com.liit.entity.OrgInfoFormMap;
import com.liit.entity.WeixinUserFormMap;
import com.liit.mapper.DictMapper;
import com.liit.mapper.IntegralDetailMapper;
import com.liit.mapper.OrgInfoMapper;
import com.liit.mapper.WeixinUserMapper;
import com.liit.plugin.PageView;
import com.liit.util.Common;
import com.liit.util.DictUtils;
import com.liit.util.IntegralUtils;
import com.liit.util.JsonUtils;
import com.liit.util.POIUtils;
import com.liit.util.RegionUtils;

@Controller
@RequestMapping("/integral/")
public class IntegralController extends BaseController {
	
	@Inject
	private OrgInfoMapper orgInfoMapper;
	
	@Inject
	private WeixinUserMapper weixinUserMapper;
	
	@Inject
	private IntegralDetailMapper integralDetailMapper;
	
	@Inject
	private DictMapper dictMapper;
	
	@RequestMapping("listOrgIntegral")
	public String listUI(Model model) throws Exception {
		model.addAttribute("res", findByRes());
		model.addAttribute("dict", DictUtils.getDictByType("SQZT"));
		return Common.BACKGROUND_PATH + "/func/orgIntegral";
	}
	
	@ResponseBody
	@RequestMapping("findOrgIntegralByPage")
	public PageView findOrgIntegralByPage(String pageNow, String pageSize) throws Exception {
		OrgInfoFormMap orgInfoFormMap = getFormMap(OrgInfoFormMap.class);
		orgInfoFormMap.put("status", Common.ORG_STATUS_PASS);
		Common.addRoleParameter(orgInfoFormMap);
		Common.trimMap(orgInfoFormMap);
		orgInfoFormMap = toFormMap(orgInfoFormMap, pageNow, pageSize, orgInfoFormMap.getStr("orderby"));
		pageView.setRecords(orgInfoMapper.findByPage(orgInfoFormMap));
		return pageView;
	}
	
	@RequestMapping("listPersonalIntegral")
	public String listPersonalIntegral(Model model) throws Exception {
		model.addAttribute("res", findByRes());
		model.addAttribute("dict", DictUtils.getDictByType("POLITICAL_TYPE"));
		return Common.BACKGROUND_PATH + "/func/personalIntegral";
	}
	
	@ResponseBody
	@RequestMapping("findPersonalIntegralByPage")
	public PageView findPersonalIntegralByPage(String pageNow, String pageSize) throws Exception {
		WeixinUserFormMap weixinUser = getFormMap(WeixinUserFormMap.class);
		Common.trimMap(weixinUser);
		weixinUser = toFormMap(weixinUser, pageNow, pageSize, weixinUser.getStr("orderby"));
		List<WeixinUserFormMap> list = weixinUserMapper.findByPage(weixinUser);
		for (WeixinUserFormMap weixinUserFormMap : list) {
			weixinUserFormMap.put("addr", StringUtils.trimToEmpty(RegionUtils.getRegionNameById(weixinUserFormMap.getStr("province"))) + " "
										+ StringUtils.trimToEmpty(RegionUtils.getRegionNameById(weixinUserFormMap.getStr("city"))) + " "
										+ StringUtils.trimToEmpty(RegionUtils.getRegionNameById(weixinUserFormMap.getStr("area"))));
		}
		pageView.setRecords(list);
		return pageView;
	}
	
	@RequestMapping("viewDetail")
	public String viewDetail(Model model, String id, String type){
		String path="";
		if("1".equals(type)){
			path="viewOrgDetail";
		}else if("2".equals(type)){
			path="viewWeixinDetail";
		}
		model.addAttribute("detailId",id);
		return Common.BACKGROUND_PATH+"/func/"+path;
	}
	@ResponseBody
	@RequestMapping("viewWeixinDetail")
	public PageView viewWeixinDetail(String pageNow, String pageSize,String id){
		IntegralDetailFormMap integralDetail=getFormMap(IntegralDetailFormMap.class);
		integralDetail.put("openid",id);
		integralDetail.put("type",0);
		integralDetail=toFormMap(integralDetail,pageNow,pageSize,integralDetail.getStr("orderby"));
		pageView.setRecords(integralDetailMapper.findByPage(integralDetail));
		return pageView;
	}
	@ResponseBody
	@RequestMapping("viewOrgDetail")
	public PageView viewOrgDetail(String pageNow, String pageSize,String id){
		IntegralDetailFormMap integralDetail=getFormMap(IntegralDetailFormMap.class);
		integralDetail.put("orgInfoId",id);
		integralDetail.put("type",1);
		integralDetail=toFormMap(integralDetail,pageNow,pageSize,integralDetail.getStr("orderby"));
		pageView.setRecords(integralDetailMapper.findByPage(integralDetail));
		return pageView;
	}
	@RequestMapping("viewGood")
	public String viewGood(Model model, String id, String type){
		model.addAttribute("detailId",id);
		return Common.BACKGROUND_PATH+"/func/viewCommodity";
	}
	@ResponseBody
	@RequestMapping("viewCommodity")
	public PageView viewCommodity(String pageNow, String pageSize,String id){
		WeixinUserFormMap weixinUser = new WeixinUserFormMap();
		weixinUser.put("openid",id);
		weixinUser=weixinUserMapper.findByNames(weixinUser).get(0);
		
		CommodityFormMap form=new CommodityFormMap();
		form.put("where","integralPrice<="+weixinUser.get("integral")+"");
		form=toFormMap(form,pageNow,pageSize,form.getStr("orderby"));
		pageView.setRecords(integralDetailMapper.findByPage(form));
		return pageView;
	}
	
	@ResponseBody
	@RequestMapping("exchange")
	public void exchage(String id,String openid){
		IntegralUtils.getInstance().commodityExchange(openid, Integer.parseInt(id));
	}
	@RequestMapping("/exportOrg")
	public void download(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String fileName = "机构积分";
		OrgInfoFormMap orgInfo = findHasHMap(OrgInfoFormMap.class);
		String exportData = orgInfo.getStr("exportData");// 列表头的json字符串

		List<Map<String, Object>> listMap = JsonUtils.parseJSONList(exportData);
		listMap.remove(listMap.get(0));
		listMap.remove(listMap.get(0));
		orgInfo.put("where","status="+Common.ORG_STATUS_PASS+"");
		List<OrgInfoFormMap> lis = orgInfoMapper.findByWhere(orgInfo);
		POIUtils.exportToExcel(response, listMap, lis, fileName);
	}
	@RequestMapping("/exportPerson")
	public void exportPerson(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String fileName = "用户积分";
		WeixinUserFormMap userFormMap = findHasHMap(WeixinUserFormMap.class);
		String exportData = userFormMap.getStr("exportData");// 列表头的json字符串

		List<Map<String, Object>> listMap = JsonUtils.parseJSONList(exportData);
		listMap.remove(listMap.get(0));
		listMap.remove(listMap.get(0));
		List<WeixinUserFormMap> lis = weixinUserMapper.findByWhere(userFormMap);//userMapper.findUserPage(userFormMap);
		if(lis.size()>0){
			for(int i=0;i<lis.size();i++){
				if("1".equals(lis.get(i).get("sex").toString())){
					lis.get(i).put("sex","男");
				}else if("2".equals(lis.get(i).get("sex").toString())){
					lis.get(i).put("sex","女");
				}else{
					lis.get(i).put("sex","未知");
				}
				
				DictFormMap dform=new DictFormMap();
				dform.put("type", "POLITICAL_TYPE");
				dform.put("value",lis.get(i).get("political"));
				if(!"".equals(lis.get(i).get("political"))&&lis.get(i).get("political")!=null){
					lis.get(i).put("political",dictMapper.findByWhere(dform).get(0).get("label"));	
				}
				
				lis.get(i).put("addr", StringUtils.trimToEmpty(RegionUtils.getRegionNameById(lis.get(i).getStr("province"))) + " "
											+ StringUtils.trimToEmpty(RegionUtils.getRegionNameById(lis.get(i).getStr("city"))) + " "
											+ StringUtils.trimToEmpty(RegionUtils.getRegionNameById(lis.get(i).getStr("area"))));
			}
		}
		POIUtils.exportToExcel(response, listMap, lis, fileName);
	}
}